package com.blueocean_health.care.management.controller;


import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.blueocean_health.care.management.common.LocalManager;
import com.blueocean_health.care.management.common.base.em.TimeType;
import com.blueocean_health.care.management.common.base.time.TimeUtils;
import com.blueocean_health.care.management.common.excel.ExcelUtil;
import com.blueocean_health.care.management.domain.base.PostageV;
import com.blueocean_health.care.management.domain.base.Selectzhdzlist;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.base.SysResultByPage;
import com.blueocean_health.care.management.domain.base.User;
import com.blueocean_health.care.management.domain.ordervo.PostageVo;
import com.blueocean_health.care.management.domain.ordervo.SelectZHDZVo;
import com.blueocean_health.care.management.em.LocalManagerType;
import com.blueocean_health.care.management.exception.OrderServiceException;
import com.blueocean_health.care.management.service.SelectZHDZService;
import com.github.pagehelper.PageInfo;
@RestController
@RequestMapping(value = "web/order")

public class SelectZHDZController {
	@Value("${zhdz.allstartdate}")
	private String startdate;
	@Autowired
    private SelectZHDZService selectZHDZService;
	@RequestMapping(value = "selectzhdz")
	public SysResult selectzhdzlist(@RequestBody(required = false) SelectZHDZVo selectZHDZVo) throws OrderServiceException{
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		selectZHDZVo.setHospitalId(user.getHospitalId());
		selectZHDZVo.setWorkPatientName(selectZHDZVo.getWorkName());
		selectZHDZVo.setWorkName("");
		 Map<String, Object> selectOrderList = selectZHDZService.selectOrderList(selectZHDZVo);
		return SysResult.ok(selectOrderList);
		
		
	}
	@RequestMapping(value = "selectzhdznew")
	public SysResult selectzhdzlistnew(@RequestBody(required = false) SelectZHDZVo selectZHDZVo) throws OrderServiceException{
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		selectZHDZVo.setHospitalId(user.getHospitalId());
		 Map<String, Object> selectOrderList = selectZHDZService.selectOrderListNew(selectZHDZVo);
		return SysResult.ok(selectOrderList);
		
		
	}
	
	@RequestMapping("/selectzhdzexcel")
	public void selectzhdzexcel(SelectZHDZVo selectZHDZVo,HttpServletResponse response) throws Exception {
		User courrentUser = LocalManager.getVal(LocalManagerType.USER_WEB);
		selectZHDZVo.setHospitalId(courrentUser.getHospitalId());
	   List<Selectzhdzlist> selectOrderexcel = selectZHDZService.selectOrderexcel(selectZHDZVo);
		String finalFileName = URLEncoder.encode("综合对账表"+TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS)+".xls", "UTF8");
		response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
		OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
		ExcelUtil.getInstance().exportObj2Excel(toClient, selectOrderexcel, Selectzhdzlist.class);
	}
	
	@RequestMapping("/selectzhdznewexcel")
	public void selectzhdznewexcel(SelectZHDZVo selectZHDZVo,HttpServletResponse response) throws Exception {
		User courrentUser = LocalManager.getVal(LocalManagerType.USER_WEB);
		selectZHDZVo.setHospitalId(courrentUser.getHospitalId());
		 Map<String, Object> selectOrderList   = selectZHDZService.selectOrderListNew(selectZHDZVo);
		 List<Selectzhdzlist> list = (List<Selectzhdzlist>)selectOrderList.get("list");
		String finalFileName = URLEncoder.encode("综合对账表"+TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS)+".xls", "UTF8");
		response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
		OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
		ExcelUtil.getInstance().exportObj2Excel(toClient, list, Selectzhdzlist.class);
	}
	@RequestMapping("/blueoceanselectzhdzexcelall")
	public void selectzhdzexcelAll(HttpServletResponse response) throws Exception {
		//User courrentUser = LocalManager.getVal(LocalManagerType.USER_WEB);
		//selectZHDZVo.setHospitalId(courrentUser.getHospitalId());
		Date date = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.MONTH, -1);
		
		String endTime = TimeUtils.getStringByDate(date, TimeType.yyyy_MM_dd);
		String startTime = TimeUtils.getStringByDate(calendar.getTime(), TimeType.yyyy_MM_dd);
		SelectZHDZVo selectZHDZVo=new SelectZHDZVo();
		selectZHDZVo.setEndTime(endTime);
		selectZHDZVo.setBeginTime(startTime);
	   List<Selectzhdzlist> selectOrderexcel = selectZHDZService.selectOrderexcel(selectZHDZVo);
		String finalFileName = URLEncoder.encode("综合对账表"+TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS)+".xls", "UTF8");
		response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
		OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
		ExcelUtil.getInstance().exportObj2Excel(toClient, selectOrderexcel, Selectzhdzlist.class);
	}
}
